package com.lagou.dao;


import com.lagou.bean.Student;
import com.lagou.util.DBUtil;

import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImp implements StudentDao {


    @Override
    public List<Student> getstd() {
        List<Student> stds = new ArrayList<Student>();
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        ResultSet rst = null;
        String sql = "select * from student ";
        try {
            pst = conn.prepareStatement(sql);

            rst = pst.executeQuery();
            while (rst.next()) {
                Student std = new Student();
                std.setId(rst.getInt("id"));
                std.setName(rst.getString("name"));
                std.setSex(rst.getString("sex"));
                std.setBirthday(rst.getDate("birthday").toString());
                std.setEmail(rst.getString("email"));
                std.setRemark(rst.getString("remark"));
                std.setCid(rst.getString("cid"));
                stds.add(std);
            }
         } catch (SQLException e) {
             e.printStackTrace();
         } finally {
            DBUtil.close(conn,pst,rst);
        }
        return stds;
    }


    @Override
    public boolean addStd(Student student) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String sql = "insert into student(id,name,sex,birthday,email,remark,cid) values (?,?,?,?,?,?,?)";;

        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, student.getId());
            pst.setString(2, student.getName());
            pst.setString(3, student.getSex());
            pst.setDate(4, new Date(sdf.parse(student.getBirthday()).getTime()));
            pst.setString(5, student.getEmail());
            pst.setString(6, student.getRemark());
            pst.setString(7, student.getCid());
            int count = pst.executeUpdate();
            return count > 0 ? true : false;
        } catch (SQLException | ParseException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,pst);
        }

        return false;
    }


    @Override
    public boolean updateStd(Student student) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        String sql = "update student set name = ?, sex = ? ,birthday = ?, email = ?, remark = ?,cid = ? where id = ?";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, student.getName());
            pst.setString(2, student.getSex());
            pst.setDate(3, new Date(sdf.parse(student.getBirthday()).getTime()));
            pst.setString(4, student.getEmail());
            pst.setString(5, student.getRemark());
            pst.setString(6, student.getCid());
            pst.setInt(7, student.getId());
            int count = pst.executeUpdate();
            return count > 0 ? true : false;
         } catch (SQLException | ParseException e) {
             e.printStackTrace();
         } finally {
            DBUtil.close(conn,pst);
        }

        return false;
    }


    @Override
    public boolean delStd(Student student) {
        Connection conn = DBUtil.getConnection();
        PreparedStatement pst = null;
        String sql = "delete from student where id = ?";

        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, student.getId());
            int count = pst.executeUpdate();
            return count > 0 ? true : false;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn,pst);
        }

        return false;

    }

}
